#!/bin/bash
#

if [ $# = 0 ]
then
echo
echo "addspacing2mhd script"
echo
echo "Add the line ElementSpacing = <spacings> to <input.mhd>"
echo 
echo "Usage: "
echo 
echo "addspacing2mhd <input.mhd> <spacingx> [<spacingy>] [<spacingz>] etc."
echo 
echo "If the .mhd already contains such a line it will be removed."
echo "instead of one .mhd-file a list of .mhd-files, enclosed by quotes (\"),"
echo "can also be used. No wildcards."
echo 
echo "NB: the input file is overwritten!!! a backup is saved as input.mhd~"
echo 
exit 64
fi

inputmhds=`ls $1`

for inputmhd in $inputmhds
do

spacings="$2 $3 $4 $5 $6 $7"

cp $inputmhd $inputmhd~

less $inputmhd~ | dos2unix | grep -v ElementDataFile | grep -v ElementSpacing | sed /^$/d > $inputmhd
echo "Adding the following line to $inputmhd :"
echo "ElementSpacing = "$spacings 
echo "ElementSpacing = "$spacings >> $inputmhd
less $inputmhd~ | dos2unix | grep ElementDataFile >> $inputmhd

done


